var purchase = {
	ajax : false,
	goods : function($obj, search_type, keyword, element, process){
		if ('main' == search_type && keyword.length > 1) {
			reg = new RegExp('^[0-9A-z-_]*$');
			if (reg.exec(keyword)) {
				if ('object' == typeof purchase.ajax) purchase.ajax.abort();
				url = cfg.url + 'goods/ajax_search/s/' + keyword;
				data = '';
				$obj.addClass('input-text-loading');
				purchase.ajax = $.post(url, data, function(result){
					//alert(result);
					if (result.r == 'SEARCH_DONE') {
						data = result.d;
						//Here we go to make elememt
						html = '';
						for (i=0; i<data.length; i++) {
							//taobao_info = eval('(' + data[i].taobao_info + ')');
							taobao_info = tool.toJson(data[i].taobao_info);
							image_url = taobao_info.image==''?site.image + 'blank.gif':taobao_info.image;
							tb_url = taobao_info.url==''?'javascript:void(0)':taobao_info.url;
							name = taobao_info.name==''?data[i].number:taobao_info.name;
							html += '<div class="each"><input type="hidden" disable="disable" class="tb-url" value="' + tb_url + '" /><div class="image"><img src="' + image_url + '" /></div><div class="number" value="' + data[i].main_goods_id + '">' + name + '</div></div>';
						}
						$('#search-result').children().remove()
						.end().append($(html))
						//Bind new event
						.children('.each').bind('click', function(){purchase.listSubGoods($(this), $(this).children('.number').attr('value'), element, process)});
					}
					else {
						$('#search-error').text('请输入有效的快搜码');
					}
					$obj.removeClass('input-text-loading');
				},'json');
			}
		}
		else {
			return false;
		}
	},
	listSubGoods : function($obj, main_goods_id, element, process){
		//Remove the showed element
		$('#search-sub-list').remove();
		url = site.url + 'goods/ajax_get_sub_goods/' + main_goods_id;
		data = '';
		style.loading('start', 'footer');
		purchase.ajax = $.post(url, data, function(result){
			//Here we go to make element
			html = '<div id="search-sub-list">';
			if (result.r == 'GET_SUB_DONE') {
				data = result.d;
				if (data.length > 0) {
					for (i=0; i<data.length; i++) {
						html += '<div class="each-sub">';
						html += '<input type="hidden" value="' + data[i].sub_goods_id + '" />';
						html += '<div class="sub-attribute">' + data[i].attribute + '</div>';
						html += '<div class="sub-number">' + data[i].number + '</div>';
						html += '<div class="sub-stock">' + data[i].stock + '件</div>';
						html += '</div>';
					}
				}
			}
			else {
				html += '错误';
			}
			html += '</div>';
			$obj.after($(html));
			//Bind the new event which add the sub goods to the application list
			$('#search-sub-list .sub-attribute').bind('click', function(){purchase.addToList($(this), element, process)});
			if (true === process) {
				purchase.process();
			}
			style.loading('end');
		}, 'json');
	},
	addToList : function($obj, element, process){
		//Product the element to insert the body
		html = '';
		html += '<div class="edit-add-each" id="sub-goods-id-' + $obj.siblings('input').val() + '">';
		html += '<input type="hidden" name="sub_goods_id[]" value="' + $obj.siblings('input').val() + '" />';
		html += '<div class="number"><a href="' + $obj.parents('#search-sub-list').prev('.each').children('.tb-url').val() + '" target="_blank">' + $obj.siblings('.sub-number').text() + '</a></div>';
		html += '<div class="attribute">' + $obj.text() + '</div>';
		html += '<div class="quantity"><input type="text" name="quantity[]" value="" /></div>';
		html += '</div>';
		$(element).append($(html));
		if (true === process) {
			$obj.parent('.each-sub').remove();
			purchase.process();
		}
		if (false === process) {
			//逻辑处理供应商 AJAX
			url = site.url + 'purchase/ajax_get_supplier_from_sub_goods/';
			data = tool.toJson('{"sub_goods_id":' + $obj.siblings('input').val() + '}');
			style.loading('start', 'footer');
			$.post(url, data, function(html){
				style.loading('end');
			});
		}
		$('input[type=submit]').unbind('click').bind('click', function(){purchase.applicationSubmit()});
		$('.edit-add-each .attribute').click(function(){$(this).parents('.edit-add-each').remove()});
	},
	//逻辑运算处理子货品序列
	process : function(){
		$obj1 = $('.edit-add-each');
		$obj2 = $('.each-sub input');
		$obj2.each(function(){
			$o = $('#sub-goods-id-' + $(this).val());
			if ($o.length > 0) $(this).parent('.each-sub').remove();
		});
		if ($('.each-sub input').length == 0) $('#search-sub-list').html('<div class="each-sub">暂无可选关联货品</div>');
	},
	applicationSubmit : function(){
		$sub_goods_ids = $('input[name="sub_goods_id[]"]');
		$quantities = $('input[name="quantity[]"]');
		if ($sub_goods_ids.length == $quantities.length) {
			data1 = tool.formToJson($sub_goods_ids, 'sub_goods_ids');
			data2 = tool.formToJson($quantities, 'quantities');
			data = tool.mergeJson(data1, data2);
			url = site.url + 'purchase/application/create/';
			$.post(url, data, function(json){
				alert(json);
			});
		}
	},
	app_list : {
		desc : function(){
			$obj = $('.each .desc');
			$obj.each(function(){
				desc = '';
				json = tool.toJson($(this).text());
				desc += '有 ' + json.length + ' 种货品 共 ';
				quantity = 0;
				for (i=0; i<json.length; i++) {
					quantity += parseInt(json[i].quantity);
				}
				desc += quantity + ' 件';
				$(this).text(desc);
			})
		},
	},
	detail : function(pur_app_id, callback){
		url = site.url + 'purchase/ajax_pur_app_detail/';
		data = tool.toJson('{"pur_app_id":"' + pur_app_id + '"}');
		$.post(url, data, function(result){
			try {
				json = tool.toJson(result);
				alert(json.data);
			}
			catch(error) {
				if (callback) {
					callback(result);
				}
				else {
					alert('callback undefined');
				}
			}
		});
	},
	action : {
		confirm : function(pur_app_id, callback) {
			url = site.url + 'purchase/ajax_purapp_confirm/';
			data = tool.toJson('{"pur_app_id":"' + pur_app_id + '"}');
			$.post(url, data, function(result){
				j = tool.toJson(result);
				if ('DONE' == j.r) {
					location.href = site.url + 'purchase/create/' + j.d;
				}
			});
		},
		denied : function(pur_app_id, callback) {
			if (!callback) {
				alert('Callback undefined!');
				return false;
			}
			url = site.url + 'purchase/ajax_purapp_denied/';
			data = tool.toJson('{"pur_app_id":"' + pur_app_id + '"}');
			$.post(url, data, function(result){
				r = tool.toJson(result);
				if ('DENIED_DONE' == r.r) {
					callback();
				}
				else {
					alert(r.d);
				}
			})
		}
	}
}

var order = {
	goods : function($obj, search_type, keyword, callback){
		if ('main' == search_type && keyword.length > 3) {
			reg = new RegExp('^[0-9A-z]*$');
			if (reg.exec(keyword)) {
				if ('object' == typeof order.ajax) order.ajax.abort();
				url = cfg.url + 'goods/ajax_search/s/' + keyword;
				data = '';
				$obj.addClass('input-text-loading');
				order.ajax = $.post(url, data, function(result){
					//alert(result);
					if (result.r == 'SEARCH_DONE') {
						data = result.d;
						//Here we go to make elememt
						html = '';
						for (i=0; i<data.length; i++) {
							//taobao_info = eval('(' + data[i].taobao_info + ')');
							taobao_info = tool.toJson(data[i].taobao_info);
							image_url = taobao_info.image==''?site.image + 'blank.gif':taobao_info.image;
							name = taobao_info.name==''?data[i].number:taobao_info.name;
							html += '<div class="each"><div class="image"><img src="' + image_url + '" /></div><div class="number" value="' + data[i].main_goods_id + '">' + name + '</div></div>';
						}
						$('#search-result').children().remove()
						.end().append($(html))
						//Bind new event
						.children('.each').bind('click', function(){order.listSubGoods($(this), $(this).children('.number').attr('value'), callback)});
					}
					else {
						$('#search-error').text('请输入有效的快搜码');
					}
					$obj.removeClass('input-text-loading');
				},'json');
			}
		}
		else {
			return false;
		}
	},
	listSubGoods : function($obj, main_goods_id, callback){
		//Remove the showed element
		$('#search-sub-list').remove();
		url = site.url + 'goods/ajax_get_sub_goods/' + main_goods_id;
		data = '';
		style.loading('start', 'footer');
		order.ajax = $.post(url, data, function(result){
			//Here we go to make element
			html = '<div id="search-sub-list">';
			if (result.r == 'GET_SUB_DONE') {
				data = result.d;
				if (data.length > 0) {
					for (i=0; i<data.length; i++) {
						html += '<div class="each-sub">';
						html += '<input type="hidden" value="' + data[i].sub_goods_id + '" />';
						html += '<div class="sub-attribute">' + data[i].attribute + '</div>';
						html += '<div class="sub-number">' + data[i].number + '</div>';
						html += '<div class="sub-stock">' + data[i].stock + '件</div>';
						html += '</div>';
					}
				}
				else {
					html += '<div class="each-sub">暂无关联的子货品</div>';
				}
			}
			else {
				html += '错误';
			}
			html += '</div>';
			$obj.after($(html));
			//Bind the new event which add the sub goods to the application list
			//$('#search-sub-list .sub-attribute').bind('click', function(){order.addToList($(this), element, process)});
			callback();
			style.loading('end');
		}, 'json');
	},
	get_supplier : function(sub_goods_id, callback) {
		url = site.url + 'purchase/ajax_get_supplier_from_sub_goods/';
		data = tool.toJson('{"sub_goods_id":' + sub_goods_id + '}');
		style.loading('start');
		$.post(url, data, function(html){
			try {
				r = tool.toJson(html);
				if ('ERROR' == r.r) {
					alert(r.d);
				}
				else {
					alert('UNKOWN ERROR');
				}
				return false;
			}
			catch(error) {
				callback(html);
			}
			style.loading('end');
		});
	}
}